﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using System.IO;

using EHTWebsite.Code;

/// <summary>
/// Author toanvt
/// CreatedDate 8/12/2011
/// </summary>
public partial class Administrator_AdminCompanyImageIU : System.Web.UI.Page
{
    public Int32 hinhAnhID = -1;
    private String message = "";

    protected void Page_Load(object sender, EventArgs e)
    {
        if (Request.QueryString["Message"] != null)
        {
            try
            {
                message = Request.QueryString["Message"].ToString();
            }
            catch (Exception)
            {
                message = "";
            }
        }

        //hiển thị Message nếu có giá trị Message gửi sang từ màn hình nguồn
        this.lbMessage.Text = message;
        this.lbMessage.CssClass = "BlueCorlor";

        if (!Page.IsPostBack)
        {
            try
            {
                hinhAnhID = Int32.Parse(Request.QueryString["HinhAnhID"].ToString());
            }
            catch
            {
                hinhAnhID = -1;
            }

            if (hinhAnhID == -1)
            {
                this.lbAdminHinhAnhCongTys.Text = "Thêm mới hình ảnh";
                this.imgHinhAnhUrl.ImageUrl = "~/ImageDatas/CompanyImages/noImage.gif";
                ViewState["action"] = HangSo.ACTION_ADD;
            }
            else
            {
                this.lbAdminHinhAnhCongTys.Text = "Sửa hình ảnh";
                HinhAnhCongTy theHinhAnhCongTy = HinhAnhCongTyProvider.GetHinhAnhCongTyByHinhAnhID(hinhAnhID);

                if (theHinhAnhCongTy != null)
                {
                    this.txtMoTaOther.Text = theHinhAnhCongTy.MoTaOther;
                    this.txtMoTaVN.Text = theHinhAnhCongTy.MoTaVN;
                    this.txtNumberOfOrder.Text = theHinhAnhCongTy.NumberOfOrder.ToString();
                    this.ckPublish.Checked = theHinhAnhCongTy.Publish;
                    this.imgHinhAnhUrl.ImageUrl = "../ImageDatas/CompanyImages/" + theHinhAnhCongTy.HinhAnhUrl;
                    ViewState["HinhAnhID"] = theHinhAnhCongTy.HinhAnhID;
                    ViewState["HinhAnhUrl"] = theHinhAnhCongTy.HinhAnhUrl;
                    ViewState["action"] = HangSo.ACTION_UPDATE;
                }
            }
        }
    }

    /// <summary>
    /// Author : toanvt
    /// CreatedDate : 07/02/2012
    /// Description : Validate Data
    /// </summary>
    /// <returns></returns>
    private Boolean ValidateData()
    {
        this.lbMessage.Text = "";
        this.lbMessage.CssClass = "";

        String message = "";
        Boolean isSuccess = true;

        if (!fuHinhAnhUrl.HasFile && ViewState["action"].ToString() == HangSo.ACTION_ADD)
        {
            isSuccess = false;
            message += "Ảnh không được để trống <br />";
        }

        if (!isSuccess)
            lbMessage.Text = message;

        return isSuccess;
    }

    protected void btnSave_Click(object sender, EventArgs e)
    {
   
        if (!ValidateData())
            return;

        Page.Validate();

        if (Page.IsValid)
        {
            HinhAnhCongTy theHinhAnhCongTy = new HinhAnhCongTy();
            Int32 hinhAnhIDOut = -1;
            theHinhAnhCongTy.MoTaOther = this.txtMoTaOther.Text;
            theHinhAnhCongTy.MoTaVN = this.txtMoTaVN.Text;
            theHinhAnhCongTy.Publish = this.ckPublish.Checked;

            try
            {
                theHinhAnhCongTy.NumberOfOrder = Int32.Parse(this.txtNumberOfOrder.Text.Trim());
            }
            catch
            {
                theHinhAnhCongTy.NumberOfOrder = 1;
            }

            if (fuHinhAnhUrl.HasFile)
            {

                String fileName = fuHinhAnhUrl.PostedFile.FileName;
                String fileType = Path.GetExtension(fileName);
                String newFileName = DateTime.Now.Ticks.ToString() + fileType;
                fuHinhAnhUrl.PostedFile.SaveAs(Server.MapPath("~/") + "//ImageDatas//CompanyImages//" + newFileName);
                this.imgHinhAnhUrl.ImageUrl = "..//ImageDatas//CompanyImages//" + newFileName;
                theHinhAnhCongTy.HinhAnhUrl = newFileName;
            }
            else
            {
                theHinhAnhCongTy.HinhAnhUrl = ViewState["HinhAnhUrl"].ToString();
            }


            String action = ViewState["action"].ToString();

            if (action == HangSo.ACTION_ADD)
            {
                theHinhAnhCongTy.HinhAnhID = -1;
                hinhAnhIDOut = HinhAnhCongTyProvider.IUHinhAnhCongTy(theHinhAnhCongTy, true);
            }
            else
            {
                theHinhAnhCongTy.HinhAnhID = Int32.Parse(ViewState["HinhAnhID"].ToString());
                hinhAnhIDOut = HinhAnhCongTyProvider.IUHinhAnhCongTy(theHinhAnhCongTy, false);
            }

            Response.Redirect("AdminCompanyImages.aspx");
        }
    }

    protected void btnBack_Click(object sender, EventArgs e)
    {
        Response.Redirect("AdminCompanyImages.aspx");
    }

    protected void btnApply_Click(object sender, EventArgs e)
    {
        if (!ValidateData())
            return;

        Page.Validate();

        if (Page.IsValid)
        {
            HinhAnhCongTy theHinhAnhCongTy = new HinhAnhCongTy();
            Int32 hinhAnhIDOut = -1;
            theHinhAnhCongTy.MoTaOther = this.txtMoTaOther.Text;
            theHinhAnhCongTy.MoTaVN = this.txtMoTaVN.Text;
            theHinhAnhCongTy.Publish = this.ckPublish.Checked;

            try
            {
                theHinhAnhCongTy.NumberOfOrder = Int32.Parse(this.txtNumberOfOrder.Text.Trim());
            }
            catch
            {
                theHinhAnhCongTy.NumberOfOrder = 1;
            }

            if (fuHinhAnhUrl.HasFile)
            {

                String fileName = fuHinhAnhUrl.PostedFile.FileName;
                String fileType = Path.GetExtension(fileName);
                String newFileName = DateTime.Now.Ticks.ToString() + fileType;
                fuHinhAnhUrl.PostedFile.SaveAs(Server.MapPath("~/") + "//ImageDatas//CompanyImages//" + newFileName);
                this.imgHinhAnhUrl.ImageUrl = "..//ImageDatas//CompanyImages//" + newFileName;
                theHinhAnhCongTy.HinhAnhUrl = newFileName;
            }
            else
            {
                theHinhAnhCongTy.HinhAnhUrl = ViewState["HinhAnhUrl"].ToString();
            }


            String action = ViewState["action"].ToString();

            if (action == HangSo.ACTION_ADD)
            {
                theHinhAnhCongTy.HinhAnhID = -1;
                hinhAnhIDOut = HinhAnhCongTyProvider.IUHinhAnhCongTy(theHinhAnhCongTy, true);
            }
            else
            {
                theHinhAnhCongTy.HinhAnhID = Int32.Parse(ViewState["HinhAnhID"].ToString());
                hinhAnhIDOut = HinhAnhCongTyProvider.IUHinhAnhCongTy(theHinhAnhCongTy, false);
            }

            Response.Redirect("AdminCompanyImageIU.aspx?HinhAnhID=" + hinhAnhIDOut.ToString() + "&Message=Lưu thành công");
        }
    }
}
